package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.item.entity.Medicinal;
import cn.zysuyuan.item.service.MedicinalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019-11-18 14:43
 */
@Api(tags = "中药材资源管理")
@RestController
@RequestMapping("medicinal")
public class MedicinalController {

    @Autowired
    private MedicinalService medicinalService;

    @ApiOperation(value = "中药材资源列表",notes = "中药材资源列表")
    @GetMapping("medicinaldata")
    public ResponseEntity<PageResult<Medicinal>> select(@RequestParam Map<String,Object> map){
        PageResult result = this.medicinalService.selectList(map);
//        if (CollectionUtils.isEmpty(result.getRows())){
//            return ResponseEntity.notFound().build();
//        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "新增药材信息", notes = "新增药材信息")
    @PostMapping("medicinaldata")
    public R save(@RequestBody Medicinal medicinal) {
        System.out.println("开始添加...");
        if (medicinalService.insert(medicinal) > 0 ) {
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "更新药材信息", notes = "更新药材信息")
    @PutMapping("medicinaldata")
    public R update(@RequestBody Medicinal medicinal) {
        System.out.println("开始更新....");
        if (medicinalService.update(medicinal) > 0) {
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "删除药材信息", notes = "删除药材信息")
    @DeleteMapping("medicinaldata")
    public R delete(
            @RequestParam(value = "id") String id,
            @RequestParam(value = "imgPath") String imgPath,
            @RequestParam(value = "mOriginImgPath") String mOriginImgPath) {
        System.out.println("开始删除...");
        if (medicinalService.delete(id,imgPath,mOriginImgPath) > 0) {
            Map<String,Object> maps = new HashMap<>();
            maps.put("flag",true);
            return R.ok(maps);
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "上传图片信息", notes = "上传图片信息")
    @PostMapping("medicinalimg")
    public ResponseEntity<Map<String,Object>> uploadImg(@RequestParam("file")MultipartFile file, @RequestParam Map<String, Object> map) {
        return ResponseEntity.status(HttpStatus.CREATED).body(medicinalService.saveImg(file,map));
    }

//    @ApiOperation(value = "测试上传表单", notes = "测试上传表单")
//    @PostMapping("test")
//    public ResponseEntity<Map<String,Object>> uploadform(@RequestParam Map<String,Object> map,@RequestParam("file")MultipartFile file) {
//
//    }
}
